;; ============== 一些通用设定 ==================
(defvar myPath (file-name-directory load-file-name)
  "本文件所处的绝对路径。")

;; ============== 软件源设定 ==================

;; 添加清华大学源ELPA
(setq package-archives '(
			 ("gnu"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
                         ("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
			 ))

(package-initialize)

(unless package-archive-contents
  (package-refresh-contents))


;; ============== 安装所有必须的软件包 ==================

;; 需要安装的软件包列表
(setq needed-packages
      '(eglot spacemacs-theme ;; 主题相关
	      pyim pyim-basedict which-key-posframe ;;输入法相关
	      use-package
	      projectile counsel-projectile project-explorer  ;; 项目管理相关
	      dashboard page-break-lines all-the-icons treemacs-all-the-icons ;; dashboard相关
	      web-mode ;; web开发模式
	      lsp-mode lsp-ui lsp-pyright lsp-java treemacs lsp-treemacs  ;; lsp相关
	      company helm helm-lsp which-key  yasnippet flycheck hydra avy  helm-xref dap-mode ;; 智能提示相关
	      )
      )

;; 安装缺失的软件包
(dolist (package needed-packages)
  (unless (package-installed-p package)
    (package-install package)))

;; 启用use-pacakge
(setq use-package-always-ensure t)
(require 'use-package)


;; ============== UI设定 ==================

;; 加载主题
(load-theme 'spacemacs-dark t)

;; 关闭启动画面
(setq inhibit-startup-message t)

;; 隐藏菜单栏工具栏滚动条
(tool-bar-mode 0)
(menu-bar-mode 0)
(scroll-bar-mode 0)

;; 显示时间
(display-time-mode 1) ;; 常显
(setq display-time-24hr-format t) ;;格式
(setq display-time-day-and-date t) ;;显示时间、星期、日期

;; 按f11全屏
(global-set-key [f11] 'toggle-frame-fullscreen)

;; 启动时最大化
(toggle-frame-maximized)

;; 透明设置
(global-set-key [(f8)] 'loop-alpha)  ;;注意这行中的F8 , 可以改成你想要的按键  
  
(setq alpha-list '((85 55) (100 100)))  
  
(defun loop-alpha ()  
  (interactive)  
  (let ((h (car alpha-list)))                  
    ((lambda (a ab)  
       (set-frame-parameter (selected-frame) 'alpha (list a ab))  
       (add-to-list 'default-frame-alist (cons 'alpha (list a ab)))  
       ) (car h) (car (cdr h)))  
    (setq alpha-list (cdr (append alpha-list (list h))))  
    )  
  )

;;(loop-alpha)

;; ================== 中文输入法相关配置 ==================

(require 'pyim)
(require 'pyim-basedict)
(require 'pyim-cregexp-utils)

;; 如果使用 popup page tooltip, 就需要加载 popup 包。
;; (require 'popup nil t)
;; (setq pyim-page-tooltip 'popup)

;; 如果使用 pyim-dregcache dcache 后端，就需要加载 pyim-dregcache 包。
;; (require 'pyim-dregcache)
;; (setq pyim-dcache-backend 'pyim-dregcache)

(pyim-basedict-enable)

(setq default-input-method "pyim")

;; 显示5个候选词。
(setq pyim-page-length 5)

;; 金手指设置，可以将光标处的编码，比如：拼音字符串，转换为中文。
(global-set-key (kbd "M-j") 'pyim-convert-string-at-point)

;; 按 "C-<return>" 将光标前的 regexp 转换为可以搜索中文的 regexp.
(define-key minibuffer-local-map (kbd "C-<return>") 'pyim-cregexp-convert-at-point)

;; 我使用全拼
(pyim-default-scheme 'quanpin)
;; (pyim-default-scheme 'wubi)
;; (pyim-default-scheme 'cangjie)

;; 我使用云拼音
(setq pyim-cloudim 'baidu)

;; pyim 探针设置
;; 设置 pyim 探针设置，这是 pyim 高级功能设置，可以实现 *无痛* 中英文切换 :-)
;; 我自己使用的中英文动态切换规则是：
;; 1. 光标只有在注释里面时，才可以输入中文。
;; 2. 光标前是汉字字符时，才能输入中文。
;; 3. 使用 M-j 快捷键，强制将光标前的拼音字符串转换为中文。
;; (setq-default pyim-english-input-switch-functions
;;               '(pyim-probe-dynamic-english
;;                 pyim-probe-isearch-mode
;;                 pyim-probe-program-mode
;;                 pyim-probe-org-structure-template))

;; (setq-default pyim-punctuation-half-width-functions
;;               '(pyim-probe-punctuation-line-beginning
;;                 pyim-probe-punctuation-after-punctuation))

;; 开启代码搜索中文功能（比如拼音，五笔码等）
(pyim-isearch-mode 1)


;; ============== 通用编辑模式配置 ==================

;; 括号配对模式
(show-paren-mode 1)
(setq-default show-paren-style 'expression)

;; 高亮显示当前行
(global-hl-line-mode t)

;; s设置默认浏览器
;;    (setq gnus-button-url 'browse-url-generic
;;          browse-url-generic-program ""
;;          browse-url-browser-function gnus-button-url)
;; (setq browse-url-browser-function 'browse-url-default-windows-browser)


;; ============== dashboard相关配置 ==================

(when (display-graphic-p)
  (require 'all-the-icons))

(set-fontset-font t 'unicode (font-spec :family "all-the-icons") nil 'append)
(set-fontset-font t 'unicode (font-spec :family "file-icons") nil 'append)
(set-fontset-font t 'unicode (font-spec :family "Material Icons") nil 'append)
(set-fontset-font t 'unicode (font-spec :family "github-octicons") nil 'append)
(set-fontset-font t 'unicode (font-spec :family "FontAwesome") nil 'append)
(set-fontset-font t 'unicode (font-spec :family "Weather Icons") nil 'append)

(require 'counsel-projectile)

(require 'dashboard)
(dashboard-setup-startup-hook)
;; Or if you use use-package
(use-package dashboard
  :ensure t
  :config
  (dashboard-setup-startup-hook))

(setq initial-buffer-choice (lambda () (get-buffer-create "*dashboard*")))

;; Set the title
(setq dashboard-banner-logo-title "欢迎使用Emacs仪表板")
;; Set the banner
(setq dashboard-startup-banner (concat  myPath  "/res/logo.png"))
;; Value can be
;; 'official which displays the official emacs logo
;; 'logo which displays an alternative emacs logo
;; 1, 2 or 3 which displays one of the text banners
;; "path/to/your/image.gif", "path/to/your/image.png" or "path/to/your/text.txt" which displays whatever gif/image/text you would prefer

;; Content is not centered by default. To center, set
(setq dashboard-center-content t)

;; To disable shortcut "jump" indicators for each section, set
(setq dashboard-show-shortcuts nil)

(setq dashboard-items '((recents  . 5)
                        (bookmarks . 5)
                        (projects . 5)
                        (agenda . 5)
                        (registers . 5)))

;;(defun dashboard-insert-custom (list-size)
;;  (insert "Custom text"))
;;(add-to-list 'dashboard-item-generators  '(custom . dashboard-insert-custom))
;;(add-to-list 'dashboard-items '(custom) t)


(setq dashboard-item-names
      '(
	("Recent Files:" . "最近文件:")
	("Bookmarks:" . "书签:")
	("Agenda for today:" . "今日议程:")
	("Agenda for the coming week:" . "议程:")
	("Registers:" . "注册者:")
	("Projects:" . "项目:")
	))

(setq dashboard-set-heading-icons t)
(setq dashboard-set-file-icons t)

(dashboard-modify-heading-icons '((recents . "file-text")
                                  (bookmarks . "book")))

(setq dashboard-set-navigator t)

;; Format: "(icon title help action face prefix suffix)"
(setq dashboard-navigator-buttons
      `(;; line1
        ((,(all-the-icons-octicon "mark-github" :height 1.1 :v-adjust 0.0)
         "我的博客"
         "浏览我的博客"
         (lambda (&rest _) (browse-url "https://www.cnblogs.com/trutheeker"))))
         ;; line 2
        ((,(all-the-icons-faicon "github" :height 1.1 :v-adjust 0.0)
          "我的代码托管"
          "前往代码托管主页"
          (lambda (&rest _) (browse-url "https://gitee.com/trutheeker")))
         )))

(setq dashboard-set-init-info t)

(setq dashboard-init-info "使用Emacs能够提高你的工作效率和生活质量！")

;;(setq dashboard-set-footer nil)
(setq dashboard-footer-messages '("不断的做下去，你会成功！如果停止脚步，将会浪费时间！时间宝贵！"))
(setq dashboard-footer-icon (all-the-icons-octicon "dashboard"
                                                   :height 1.1
                                                   :v-adjust -0.05
                                                   :face 'font-lock-keyword-face))

(setq dashboard-projects-switch-function 'counsel-projectile-switch-project-by-name)

;;(add-to-list 'dashboard-items '(agenda) t)
;;(setq dashboard-week-agenda t)
(setq dashboard-filter-agenda-entry 'dashboard-no-filter-agenda)


;; ============== 代码提示功能相关模块加载 ==================

(use-package projectile)
(use-package flycheck)
(use-package yasnippet :config (yas-global-mode))
(use-package lsp-mode :hook ((lsp-mode . lsp-enable-which-key-integration)))
(use-package hydra)
(use-package company)
(use-package lsp-ui)
(use-package which-key :config (which-key-mode))
(use-package lsp-java :config (add-hook 'java-mode-hook 'lsp))
(use-package dap-mode :after lsp-mode :config (dap-auto-configure-mode))
(use-package dap-java :ensure nil)
(use-package helm-lsp)
(use-package helm
  :config (helm-mode))
(use-package lsp-treemacs)


;; ============== lsp-mode相关配置 ==============

(setq treemacs-project-follow-mode t)
;; 添加 lsp的忽略目录
(with-eval-after-load 'lsp-mode
  (add-to-list 'lsp-file-watch-ignored-directories "~")
  )

(global-set-key (kbd "<f1>") 'treemacs)
(global-set-key (kbd "<f2>") 'lsp-treemacs-symbols)


;; ================== ELisp语言模式配置 ==================

(add-hook 'emacs-lisp-mode-hook 'linum-mode)


;; ============== Python模式配置 ==============

(load-file (concat  myPath  "python.el"))


;; ============== JAVA模式配置 ==============

(load-file (concat  myPath  "java.el"))
;;(load-file (concat  myPath  "java-test.el"))


;; ============== WEB模式配置 ==============

(load-file (concat  myPath  "web.el"))



;; ============== C/C++模式配置 ==============

(load-file (concat  myPath  "c-cpp.el"))


(defun g-org-generate-docx-file()
  (interactive)
  (shell-command
   (concat "pandoc -o \""
	   (replace-regexp-in-string ".org$" ".docx" buffer-file-name)
	   "\"" " \"" buffer-file-name "\"")
   )
  )

(global-set-key (kbd "<f10>") 'g-org-generate-docx-file)
